package com.uchonor.api.core.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.uchonor.api.core.entity.DatasourceConfig;
import com.uchonor.api.core.service.DatasourceConfigService;
import com.uchonor.olight.agent.annotation.Server;
import com.uchonor.olight.agent.annotation.web.*;
import com.uchonor.olight.agent.module.web.BaseDatagridController;
import com.uchonor.olight.agent.module.web.vo.RspResult;
import com.uchonor.olight.agent.module.web.vo.ServiceResult;
import com.uchonor.olight.agent.module.web.vo.StatusCode;

@Server(path = "/datasource")
public class DatasourceConfigController extends BaseDatagridController<DatasourceConfigService, DatasourceConfig> {
    @Get(path = "/allList")
    public ServiceResult allList() {
        ServiceResult sr = new ServiceResult();
        RspResult rr = new RspResult();
        try {
            QueryWrapper<DatasourceConfig> queryWrapper = Wrappers.query();
            queryWrapper.orderByDesc("update_time");
            rr.setDATA(this.baseService.list(queryWrapper));
        } catch (Exception e) {
            rr.setRSP_DESC(e.getMessage());
            rr.setRSP_CODE(StatusCode.CODE_3000.getKey());
        }
        sr.setRSP(rr);
        return sr;
    }

    @Delete(path = "/deleteById")
    public ServiceResult deleteById(@RequestParam(name = "id") String datasourceConfigId) {
        ServiceResult sr = new ServiceResult();
        RspResult rr = new RspResult();
        try {
            this.baseService.deleteDatasourceConfig(datasourceConfigId);
        } catch (Exception e) {
            rr.setRSP_DESC(e.getMessage());
            rr.setRSP_CODE(StatusCode.CODE_3000.getKey());
        }
        sr.setRSP(rr);
        return sr;
    }

    @Post(path = "/save")
    public ServiceResult save(@RequestBody DatasourceConfig datasourceConfig){
        ServiceResult sr = new ServiceResult();
        RspResult rr = new RspResult();
        try {
            setCreateUser(datasourceConfig);
            rr.setDATA(this.baseService.saveDatasourceConfig(datasourceConfig));
        } catch (Exception e) {
            rr.setRSP_DESC(e.getMessage());
            rr.setRSP_CODE(StatusCode.CODE_3000.getKey());
        }
        sr.setRSP(rr);
        return sr;
    }
}
